﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// WindowTeshuItemAdd.xaml 的交互逻辑
    /// </summary>
    public partial class WindowTeshuItemAdd : Window
    {
        public WindowTeshuItemAdd()
        {
            InitializeComponent();
        }
        TeshuBusiness teshuBusiness = new TeshuBusiness();

        /// <summary>
        /// 主表id
        /// </summary>
        internal long teshuGasKey = 0;

        //保存   //创建时间：2022-12-21 10:36:55。
        private void save_Click(object sender, RoutedEventArgs e)
        {
            Teshu_gas_item info = new Teshu_gas_item();
            info.Analysis_time = this.date_analytime.Text;
            info.H2s_mg_m3 = this.txt_H2S.Text.ToMyDecimal();
            info.S_mg_m3 = this.txt_s_mg_m3.Text.ToMyDecimal();
            info.Teshu_gas_id = teshuGasKey;

            var result = teshuBusiness.AddTeshu_gasItem(info);
            ShowAlertHelpter.ShowAlert(result.Msg);
            if (result.Code==(int)ResultCode.Success)
            {
                //触发刷新列表
                RefreshParentWindowListData(sender, e);
            }
        }

        /// <summary>
        /// 刷新父窗体中Teshu_gas_item表格数据
        /// </summary>
        /// 创建时间：2022-12-21 11:03:21。作者：王浩力
        public event EventHandler RefreshParentWindowListData;


        //检查输入字符串是否为数字 //创建时间：2022-12-21 10:38:40，作者：王浩力
        private void numberCheck_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textElem = (sender as TextBox);

            //解决连续输入很多.
            if ((
                textElem.Text.StartsWith(".") || string.IsNullOrWhiteSpace(textElem.Text)
                || textElem.Text.Contains(".")
                ) && e.Text == ".")
            {
                e.Handled = true;
                return;
            }

            //禁止在0前面继续输入0
            int index = ((System.Windows.Controls.TextBox)e.Source).CaretIndex;
            if (textElem.Text.StartsWith("0.") && index == 0 && e.Text == "0")
            {
                e.Handled = true;
                return;
            }

            //不能输入非数字
            if (Regex.IsMatch(e.Text, @"[^\d+\.{0,1}\d+]+"))
            {
                e.Handled = true;
                return;
            }
        }

        //检查输入字符串是否为数字 //创建时间：2022-12-21 10:38:40，作者：王浩力
        private void numberCheck_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                if (textBox.Text == "0.0" || textBox.Text.StartsWith("00"))
                {
                    textBox.Text = textBox.Text.Replace("00", "");
                }
                if (!textBox.Text.StartsWith("0.") && Regex.IsMatch(textBox.Text, @"0+[1-9]+\.*\d+", RegexOptions.IgnoreCase))
                {
                    textBox.Text = textBox.Text.TrimStart('0');
                }

                //输入的不是小数或正整数           
                Regex regex = new Regex(@"[^\d+\.{0,1}\d+]", RegexOptions.IgnoreCase);
                textBox.Text = regex.Replace(textBox.Text, "");
            }
        }

    }
}
